//
//  TableContainer.swift
//  jocool
//
//  Created by tong on 16/6/17.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import Foundation
import Eelay

open class TableContainer: PaginatorController {    
    
    override open var sections:TP.section{
        get{
            let sec:TP.section = [
                []
            ]
            return sec
        }
    }
    
    override open var selector:TP.selector{
        get{
            let sel = [
                "<default>":cellType.self,
            ]
            return sel
        }
    }
    
    public var content:TermsContainer?{
        didSet{
            switch self.content! {
            case .table(title: let title,emptyMsg:let emptyMsg, net:let net,let params, node: let node, cellType: let cellType, cellHeigth: let cellHeigth, detailType: let detailType,  test:let test):
                self.title = title
                self.cellType = cellType
                self.detailType = detailType
                if let emptyMessage = emptyMsg
                {
                    self.paginator.empty_msg = emptyMessage
                }
                
                self.cellHeigth = cellHeigth
                if let cellH = cellHeigth
                {
                    tableView.rowHeight = cellH
                }
                else
                {
                    tableView.buildDynamicHeight()
                }
                
                
                if let p = params
                {
                    paginator.parameters = p
                }
                
                paginator.net = net
                paginator.node = node
                paginator.type = .full
                paginator.in_test = test
            case .list(net: let net, node: let node, emptyMsg:let emptyMsg, params: let params, cellType: let cellType, detailType: let detailType, test: let test):
                if let cellTp = cellType as? UITableViewCell.Type
                {
                    self.cellType = cellTp
                }
                self.detailType = detailType
                if let emptyMessage = emptyMsg
                {
                    self.paginator.empty_msg = emptyMessage
                }
                
                if let p = params
                {
                    paginator.parameters = p
                }
                
                paginator.net = net
                paginator.node = node
                paginator.type = .full
                paginator.in_test = test
            default:
                break
            }
            
        }
    }
    
    public var cellHeigth:CGFloat?
    
    public var detailType:UIViewController.Type?
    
    public var cellType:UITableViewCell.Type = JoTableCell.self
    
    public var didSelect:((NSMutableDictionary,TableContainer) ->Void)?
    
    public var back_color:UIColor = .table_bg_color
    
    public var viewDidLoadAction:((TableContainer)->Void)? = nil
    
    
    public required init()
    {
        super.init()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override open func viewDidLoad() {
        
        super.viewDidLoad()
        
        tableView.separatorStyle = .none
        
        
        let v = UIView()
        v.bsui.background(color: back_color)
        tableView.backgroundView = v
        tableView.backgroundColor = back_color
        self.viewDidLoadAction?(self)
    }
    
    override open func reload_page() {
        
        loadMore(page: self.paginator.page_begin)//            refresh()
        load(sections: sections, selector: selector)
        tableView.reloadData()
        loadingV.loading()
    }
    
    open override func mo_request_alert
        (
        msg:String = "请检查网络链接是否正常".loc,
        title:String = "".loc,
        bt_title:String = "".loc
        )
    {
        weak var wself = self
        if bt_title.len < 1
        {
            loadingV.show(message: msg, title: title)
            return
        }
        self.loadingV.handle(message: msg, title: title, button: bt_title, handAction: {
            wself?.loadingV.loading()
            wself?.loadMore(page: (wself?.paginator.page_begin ?? PageConfig.page_begin))
//            wself?.pageRequest()
            //            wself?.refresh()
        })
    }

    
    
    open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let obj = tableView.cellData(at: indexPath)
        {
            if let detailT = self.detailType
            {
                let ctr = detailT.init()
                if let mctr = ctr as? MoAtrributeProtocol
                {
                    mctr.mo.model = obj
                }
                self.navigationController?.pushViewController(ctr, animated: true)
            }
            if let selectA = self.didSelect
            {
                selectA(obj,self)
            }
            
        }
    }
    
  
    
}



